﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV.Database;
using System.Linq;

namespace QLTV.UserControl
{
    public partial class ucAssignment : DevExpress.XtraEditors.XtraUserControl
    {
        public ucAssignment()
        {
            InitializeComponent();
            //rdoStatus.SelectedIndex = 4;
            dDatetime1.DateTime = DateTime.Now;
            dDatetime3.DateTime = dDatetime1.DateTime.AddMonths(int.Parse(dDatetime2.Value.ToString()));
           
        }

        //public ucAssignment(List<string> CrewsId)
        //{
        //    InitializeComponent();
           
        //}

        public List<string> crewList;
        AssignmentDataContext data = new AssignmentDataContext();

        private void rdoStatus_Properties_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dsVessel.tblVessel.Count == 0)
            {
                this.tblVesselTableAdapter.Fill(dsVessel.tblVessel);
            }
            switch (rdoStatus.SelectedIndex)
            {
                case 0:
                    layoutDatetime1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                    layoutDatetime2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                    layoutVessel.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                    dDatetime3.Enabled = false;
                    layoutDatetime1.Text = ((BaseForm)ParentForm).rm.GetString("sDateOnBoard", ((BaseForm)ParentForm).myCultureInfo);//Thời gian lên tàu
                    layoutDatetime2.Text = ((BaseForm)ParentForm).rm.GetString("sDateContract", ((BaseForm)ParentForm).myCultureInfo);//Thời gian hợp đồng
                    layoutDatetime3.Text = ((BaseForm)ParentForm).rm.GetString("sDateLeft", ((BaseForm)ParentForm).myCultureInfo);//Thời gian dự kiến rời tàu
                   tblVesselBindingSource.Position = 0;
                   QLTV.Database.dsVessel.tblVesselRow row = dsVessel.tblVessel[0];
                    lkVessel.EditValue = row.nVesselId;
                    break;
                case 1:
                    layoutDatetime1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                    layoutDatetime2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                    layoutVessel.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    dDatetime3.Enabled = false;
                    layoutDatetime1.Text = ((BaseForm)ParentForm).rm.GetString("sDateLeftOnBoard", ((BaseForm)ParentForm).myCultureInfo);//Thời gian rời tàu
                    layoutDatetime2.Text = ((BaseForm)ParentForm).rm.GetString("sDateRequired", ((BaseForm)ParentForm).myCultureInfo);//Thời gian yêu cầu nghỉ
                    layoutDatetime3.Text = ((BaseForm)ParentForm).rm.GetString("sDateReady", ((BaseForm)ParentForm).myCultureInfo);//Thời gian dự kiến sẵn sàng lên tàu
                    break;
                case 2:
                    layoutDatetime1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    layoutDatetime2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    layoutVessel.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    dDatetime3.Enabled = true;
                    layoutDatetime3.Text = ((BaseForm)ParentForm).rm.GetString("sDateExprieContract", ((BaseForm)ParentForm).myCultureInfo);//Thời điểm cắt hợp đồng
                    break;
                case 3:
                   layoutDatetime1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    layoutDatetime2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    layoutVessel.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    dDatetime3.Enabled = true;
                    layoutDatetime3.Text = ((BaseForm)ParentForm).rm.GetString("sDateRetired", ((BaseForm)ParentForm).myCultureInfo);//Thời điểm nghỉ hưu
                    break;
                case 4:
                    layoutDatetime1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    layoutDatetime2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    layoutVessel.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    dDatetime3.Enabled = true;
                    layoutDatetime3.Text = ((BaseForm)ParentForm).rm.GetString("sDateRegisterContract", ((BaseForm)ParentForm).myCultureInfo);//Thời điểm ký hợp đồng tuyển dụng
                    break;
                default:
                    break;
            }
        }

        private void ucAssignment_Load(object sender, EventArgs e)
        {
            if (crewList != null && crewList.Count != 0)
            {
                tblAssignment a = data.tblAssignments.SingleOrDefault(u => u.nCrewid == crewList.FirstOrDefault() && u.isActive == true);
                if (a != null)
                {
                    switch (a.cStatus.ToUpper())
                    {
                        case "ON BOARD":
                            rdoStatus.SelectedIndex = 0;
                            dDatetime1.EditValue = a.dEmbarked;
                            dDatetime2.EditValue = a.nContract;
                            dDatetime3.EditValue = a.dDuKienRoiTau;
                            lkVessel.EditValue = a.nVesselId;
                            cGhiChuMemoEdit.EditValue = a.cGhiChu;
                            break;
                        case "ON VACATION":
                            rdoStatus.SelectedIndex = 1;
                            dDatetime1.EditValue = a.dLeave;
                            dDatetime2.EditValue = a.nRequest;
                            dDatetime3.EditValue = a.dSanSangLenTau;
                            cGhiChuMemoEdit.EditValue = a.cGhiChu;
                            break;
                        case "ON RESIGN":
                            rdoStatus.SelectedIndex = 2;
                            dDatetime3.EditValue = a.dCatHopDong;
                            cGhiChuMemoEdit.EditValue = a.cGhiChu;
                            break;
                        case "ON RETIRE":
                            rdoStatus.SelectedIndex = 3;
                            dDatetime3.EditValue = a.dNghiHuu;
                            cGhiChuMemoEdit.EditValue = a.cGhiChu;
                            break;
                        case "NEW CREW":
                            rdoStatus.SelectedIndex = 4;
                            dDatetime3.EditValue = a.dKyHopDong;
                            cGhiChuMemoEdit.EditValue = a.cGhiChu;
                            break;
                        default:
                            break;
                    }
                }
                
            }

            this.tblVesselTableAdapter.Fill(dsVessel.tblVessel);
        }

        private void dDatetime1_Properties_DateTimeChanged(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(dDatetime2.Value.ToString()))
            {
                dDatetime3.DateTime = dDatetime1.DateTime.AddMonths(int.Parse(dDatetime2.Value.ToString()));
            }
        }

        private void dDatetime2_Properties_ValueChanged(object sender, EventArgs e)
        {
            if (dDatetime1.DateTime != null)
            {
                dDatetime3.DateTime = dDatetime1.DateTime.AddMonths(int.Parse(dDatetime2.Value.ToString()));
            }
        }

        public bool btnSaveVisible
        {
            get
            {
                return layoutControlItem6.IsHidden;
            }
            set
            {
                if (value)
                {
                    layoutControlItem6.ShowInCustomizationForm = true;
                }
                else
                {
                    layoutControlItem6.HideToCustomization();
                }
            }
        }

        public bool btnCancelVisible
        {
            get
            {
                return layoutControlItem7.IsHidden;
            }
            set
            {
                if (value)
                {
                    layoutControlItem7.ShowInCustomizationForm = true;
                }
                else
                {
                    layoutControlItem7.HideToCustomization();
                }
              
            }
        }

        #region Handler
        public delegate void SaveHandler(object sender, EventArgs e);
        public event SaveHandler SaveClick;
        void onSaveClick(object sender, EventArgs e)
        {
            if (SaveClick != null)
            {
                SaveClick(sender, e);
            }
        }
        public void saveAssignment(object sender, EventArgs e)
        {
          
            List<tblAssignment> Assignments = new List<tblAssignment>();
            foreach (string CrewId in this.crewList)
            {
                var query = from a in data.tblAssignments
                            where a.nCrewid == CrewId
                            select a;
                if (query != null && query.Count() == 5)
                {
                    foreach (tblAssignment a in query)
                    {
                        a.isActive = false;
                        Assignments.Add(a);
                    }
                }
                else
                {
                    if (query != null)
                    {
                        data.tblAssignments.DeleteAllOnSubmit(query);
                    }
                    tblAssignment a1 = new tblAssignment();
                    tblAssignment a2 = new tblAssignment();
                    tblAssignment a3 = new tblAssignment();
                    tblAssignment a4 = new tblAssignment();
                    tblAssignment a5 = new tblAssignment();

                    a1.cStatus = "On Board";
                    a1.nCrewid = CrewId;
                    a1.isActive = false;

                    a2.cStatus = "On Vacation";
                    a2.nCrewid = CrewId;
                    a2.isActive = false;

                    a3.cStatus = "On Resign";
                    a3.nCrewid = CrewId;
                    a3.isActive = false;

                    a4.cStatus = "On Retire";
                    a4.nCrewid = CrewId;
                    a4.isActive = false;

                    a5.cStatus = "New Crew";
                    a5.nCrewid = CrewId;
                    a5.isActive = false;

                    Assignments.Add(a1);
                    Assignments.Add(a2);
                    Assignments.Add(a3);
                    Assignments.Add(a4);
                    Assignments.Add(a5);
                    data.tblAssignments.InsertAllOnSubmit(Assignments);
                }
            }
            

            switch (rdoStatus.SelectedIndex)
            {
                case 0:
                    foreach (tblAssignment assignment in Assignments)
                    {
                        if (assignment.cStatus.ToUpper() == "ON BOARD")
                        {
                            tblAssignmentLog log = new tblAssignmentLog();
                            assignment.isActive = true;
                            assignment.dEmbarked = dDatetime1.DateTime;
                            assignment.nContract = int.Parse(dDatetime2.Value.ToString());
                            assignment.nVesselId = int.Parse(lkVessel.EditValue.ToString());

                            log.cStatus = assignment.cStatus;
                            log.nCrewid = assignment.nCrewid;
                            log.dModified = DateTime.Now;
                            log.nVesselId = assignment.nVesselId;
                            log.dEmbarked = dDatetime1.DateTime;
                            log.nContract = int.Parse(dDatetime2.Value.ToString());
                            log.nVesselId = int.Parse(lkVessel.EditValue.ToString());
                            if (cGhiChuMemoEdit.EditValue != null)
                            {
                                assignment.cGhiChu = cGhiChuMemoEdit.EditValue.ToString();
                                log.cGhiChu = cGhiChuMemoEdit.EditValue.ToString();
                            }
                            assignment.dDuKienRoiTau = dDatetime3.DateTime;
                            log.dDuKienRoiTau = dDatetime3.DateTime;

                            data.tblAssignmentLogs.InsertOnSubmit(log);
                            alertControl1.Show((BaseForm)ParentForm, ((BaseForm)ParentForm).rm.GetString("sMessageCaption", ((BaseForm)ParentForm).myCultureInfo), ((BaseForm)ParentForm).rm.GetString("sMessageSuccess", ((BaseForm)ParentForm).myCultureInfo));
                        }
                    }
                    break;
                case 1:
                    foreach (tblAssignment assignment in Assignments)
                    {
                        if (assignment.cStatus.ToUpper() == "ON VACATION")
                        {
                            tblAssignment onboard = data.tblAssignments.SingleOrDefault(u => u.nCrewid == assignment.nCrewid && u.cStatus.ToUpper() == "ON BOARD" && u.isActive == true);
                            tblAssignment current1 = data.tblAssignments.SingleOrDefault(u => u.isActive == true && u.nCrewid == assignment.nCrewid);

                            if (current1 != null)
                            {
                                current1.isActive = false;

                                if (onboard != null)
                                {
                                    tblRanking rank = (from r in data.tblRankings
                                                       orderby r.dDateIssused descending
                                                       where r.nCrewId == current1.nCrewid
                                                       select r).FirstOrDefault();
                                    tblExprience exp = new tblExprience();
                                    exp.nCrewId = current1.nCrewid;
                                    exp.dEmbarkedDate = current1.dEmbarked;
                                    exp.dDisembarkedDate = current1.dDuKienRoiTau;
                                    exp.nVesselId = current1.nVesselId;
                                    exp.fDuration = current1.nContract;

                                    if (rank != null)
                                    {
                                        exp.nRankId = rank.rankID;
                                    }

                                    data.tblExpriences.InsertOnSubmit(exp);
                                }
                            }

                            tblAssignmentLog log = new tblAssignmentLog();

                            assignment.isActive = true;
                            assignment.dLeave = dDatetime1.DateTime;
                            assignment.nRequest = int.Parse(dDatetime2.Value.ToString());

                            log.cStatus = assignment.cStatus;
                            log.nCrewid = assignment.nCrewid;
                            log.dModified = DateTime.Now;
                            log.dLeave = dDatetime1.DateTime;
                            log.nRequest = int.Parse(dDatetime2.Value.ToString());
                            if (cGhiChuMemoEdit.EditValue != null)
                            {
                                assignment.cGhiChu = cGhiChuMemoEdit.EditValue.ToString();
                                log.cGhiChu = cGhiChuMemoEdit.EditValue.ToString();
                            }

                            assignment.dSanSangLenTau = dDatetime3.DateTime;
                            log.dSanSangLenTau = dDatetime3.DateTime;
                            data.tblAssignmentLogs.InsertOnSubmit(log);
                            alertControl1.Show((BaseForm)ParentForm, ((BaseForm)ParentForm).rm.GetString("sMessageCaption", ((BaseForm)ParentForm).myCultureInfo), ((BaseForm)ParentForm).rm.GetString("sMessageSuccess", ((BaseForm)ParentForm).myCultureInfo));
                        }
                        //else
                        //{
                        //    tblAssignment current = data.tblAssignments.SingleOrDefault(u => u.isActive == true && u.nCrewid == assignment.nCrewid);
                        //    if (current != null && assignment.cStatus == current.cStatus)
                        //    {
                        //        assignment.cDescription = current.cDescription;
                        //        assignment.cStatus = current.cStatus;
                        //        assignment.dCatHopDong = current.dCatHopDong;
                        //        assignment.dDuKienRoiTau = current.dDuKienRoiTau;
                        //        assignment.dEmbarked = current.dEmbarked;
                        //        assignment.dKyHopDong = current.dKyHopDong;
                        //        assignment.dLeave = current.dLeave;
                        //        assignment.dModified = current.dModified;
                        //        assignment.dNghiHuu = current.dNghiHuu;
                        //        assignment.dSanSangLenTau = current.dSanSangLenTau;
                        //        assignment.isActive = true;
                        //        assignment.nContract = current.nContract;
                        //        assignment.nCrewid = current.nCrewid;
                        //        assignment.nRequest = current.nRequest;
                        //        assignment.nVesselId = current.nVesselId;
                        //        assignment.cGhiChu = current.cGhiChu;
                        //    }
                        //}
                    }
                    break;
                case 2:
                    foreach (tblAssignment assignment in Assignments)
                    {
                        if (assignment.cStatus.ToUpper() == "ON RESIGN")
                        {
                            tblAssignmentLog log = new tblAssignmentLog();
                            assignment.isActive = true;
                            assignment.dCatHopDong = dDatetime3.DateTime;

                            log.cStatus = assignment.cStatus;
                            log.dCatHopDong = dDatetime3.DateTime;
                            log.nCrewid = assignment.nCrewid;
                            log.dModified = DateTime.Now;
                            if (cGhiChuMemoEdit.EditValue != null)
                            {
                                assignment.cGhiChu = cGhiChuMemoEdit.EditValue.ToString();
                                log.cGhiChu = cGhiChuMemoEdit.EditValue.ToString();
                            }
                            data.tblAssignmentLogs.InsertOnSubmit(log);
                            alertControl1.Show((BaseForm)ParentForm, ((BaseForm)ParentForm).rm.GetString("sMessageCaption", ((BaseForm)ParentForm).myCultureInfo), ((BaseForm)ParentForm).rm.GetString("sMessageSuccess", ((BaseForm)ParentForm).myCultureInfo));
                        }
                    }
                    break;
                case 3:
                    foreach (tblAssignment assignment in Assignments)
                    {
                        if (assignment.cStatus.ToUpper() == "ON RETIRE")
                        {
                            tblAssignmentLog log = new tblAssignmentLog();
                            assignment.isActive = true;
                            assignment.dNghiHuu = dDatetime3.DateTime;

                            log.cStatus = assignment.cStatus;
                            log.dNghiHuu = dDatetime3.DateTime;
                            log.nCrewid = assignment.nCrewid;
                            log.dModified = DateTime.Now;
                            if (cGhiChuMemoEdit.EditValue != null)
                            {
                                assignment.cGhiChu = cGhiChuMemoEdit.EditValue.ToString();
                                log.cGhiChu = cGhiChuMemoEdit.EditValue.ToString();
                            }
                            data.tblAssignmentLogs.InsertOnSubmit(log);
                            alertControl1.Show((BaseForm)ParentForm, ((BaseForm)ParentForm).rm.GetString("sMessageCaption", ((BaseForm)ParentForm).myCultureInfo), ((BaseForm)ParentForm).rm.GetString("sMessageSuccess", ((BaseForm)ParentForm).myCultureInfo));
                        }
                    }
                    break;
                case 4:
                    foreach (tblAssignment assignment in Assignments)
                    {
                        if (assignment.cStatus.ToUpper() == "NEW CREW")
                        {
                            tblAssignmentLog log = new tblAssignmentLog();
                            assignment.isActive = true;
                            assignment.dKyHopDong = dDatetime3.DateTime;

                            log.cStatus = assignment.cStatus;
                            log.dKyHopDong = dDatetime3.DateTime;
                            log.nCrewid = assignment.nCrewid;
                            log.dModified = DateTime.Now;
                            if (cGhiChuMemoEdit.EditValue != null)
                            {
                                assignment.cGhiChu = cGhiChuMemoEdit.EditValue.ToString();
                                log.cGhiChu = cGhiChuMemoEdit.EditValue.ToString();
                            }
                            data.tblAssignmentLogs.InsertOnSubmit(log);
                            alertControl1.Show((BaseForm)ParentForm, ((BaseForm)ParentForm).rm.GetString("sMessageCaption", ((BaseForm)ParentForm).myCultureInfo), ((BaseForm)ParentForm).rm.GetString("sMessageSuccess", ((BaseForm)ParentForm).myCultureInfo));
                        }
                    }
                    break;
                default:
                    break;
            }
            data.SubmitChanges();
            onSaveClick(sender, e);
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            saveAssignment(sender,e);
           
        }

        public delegate void CancelHandler(object sender, EventArgs e);
        public event CancelHandler CancelClick;
        void onCancelClick(object sender, EventArgs e)
        {
            if (CancelClick != null)
            {
                CancelClick(sender, e);
            }
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            onCancelClick(sender, e);
        } 

        #endregion

     
    }
}
